package com.fengye.rabbit.order.rabbit;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;



/**
 * 订单 - 配置死信队列
 * @author sky
 * @date 2021年06月10日 1:23 下午
 */
@Configuration
public class RabbitConfig { 

    @Value("${order.dead.queue.name}")
    private String deadLetterQueueName;

    @Value("${order.dead.exchange.name}")
    private String deadLetterExchangeName;

    @Value("${order.dead.routing.key.name}")
    private String deadRoutingKeyName;


    @Value("${order.producer.basic.exchange.name}")
    private String orderProducerExchangeName;

    @Value("${order.producer.basic.routing.key.name}")
    private String orderProducerRoutingKeyName;

    @Value("${order.consumer.real.queue.name}")
    private String orderConsumerQueueName;
    /**
     * 死信队列
     * @return org.springframework.amqp.core.Queue
     */
    @Bean("orderDeadQueue")
    public Queue basicDeadQueue() {
        Map<String, Object> args = new HashMap<>();

        // 死信交换机名称
        args.put("x-dead-letter-exchange", deadLetterExchangeName);

        // 死信路由
        args.put("x-dead-letter-routing-key", deadRoutingKeyName);

        // 设置 TTL，单位是 ms，10000ms = 10s
        args.put("x-message-ttl", 3000);

        return new Queue(deadLetterQueueName, true, false, false, args);
    }

    /**
     * 创建交换机 - 面向生产者
     * @return org.springframework.amqp.core.TopicExchange
     */
    @Bean("orderProducerExchange")
    public TopicExchange orderProducerExchange() {
        return new TopicExchange(orderProducerExchangeName, true, false);
    }

    /**
     * 创建"基本消息模型"的基本绑定 - 基本交换机+基本路由 （面向生产者）
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding basicProducerBinding(@Qualifier("orderDeadQueue") Queue orderDeadQueue, @Qualifier("orderProducerExchange") TopicExchange orderProducerExchange) {
        return BindingBuilder.bind(orderDeadQueue).to(orderProducerExchange).with(orderProducerRoutingKeyName);
    }




    /**
     * 创建真正的队列，面向消费者
     * @return org.springframework.amqp.core.Queue
     */
    @Bean
    public Queue orderConsumerQueue() {
        return new Queue(orderConsumerQueueName,true);
    }


    /**
     * 创建真正的交换机,面向消费者
     * @return org.springframework.amqp.core.TopicExchange
     */
    @Bean
    public TopicExchange orderDeadExchange() {
        return new TopicExchange(deadLetterExchangeName,true,false);
    }


    /**
     * 创建死信路由及其绑定
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding basicDeadBinding() {
        return BindingBuilder.bind(orderConsumerQueue()).to(orderDeadExchange()).with(deadRoutingKeyName);
    }

    @Autowired
    private CachingConnectionFactory cachingConnectionFactory;
    @Bean(name = "singleListenerContainer")
    public SimpleRabbitListenerContainerFactory listenerContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(cachingConnectionFactory);
        // 设置消息在传输中的格式，在这里采用 JSON 格式进行传输
        factory.setMessageConverter(new Jackson2JsonMessageConverter());

        // 设置并发消费者示例的初始数量
        factory.setConcurrentConsumers(1);

        // 设置并发消费者示例的最大数量
        factory.setMaxConcurrentConsumers(1);

        // 设置并发消费者示例中每个示例拉取的数量
        factory.setPrefetchCount(1);
        return factory;
    }
}

